﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Admintool.Infrastructure;

namespace Admintool
{
    partial class Program
    {
        [Command("\tget db for uid")]
        public static void GetDB4UID([Required(Description = "uid")]string uid)
        {
            const string DB_UserInfo = "db_user_info";
            const string DB_UserInfo_Table = "t_user_info";
            ConsoleHelper.ShowMessage("uid: {0} is in {1}", ConsoleColor.Green, uid, GetDBPartion(uid, DB_UserInfo, DB_UserInfo_Table));

        }

         static string GetDBPartion(string uid, string db, string table, int dbNum = 10, int tableNum = 10)
        {
            long v = Key2RouteKey(uid) % (dbNum * tableNum);
            return string.Format("{0}.{1}",
                dbNum == 1 ? db : string.Format("{0}_{1}", db, v / tableNum % dbNum),
                tableNum == 1 ? table : string.Format("{0}_{1}", table, v % tableNum));
        }

         static long Key2RouteKey(string key)
        {
            return Convert.ToInt64(key.Substring(5, key.Length - 5 - 1)) % 32687;
        }



    }
}
